<?php
/**
 * 首页焦点图、达人推荐 -- 上下架排序功能提交程序
 * 
 * @copyright zhanghuan
 * @since 2012-03-20
 */


include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/index/db_food_focus_pic_index.class.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/index/db_food_user_daren_index.class.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	cgi::both($g_cgival["type"], "type", "");
	$g_cgival["type"] = trim($g_cgival["type"]);
	
	cgi::both($g_cgival["op"], "op", "up");
	if ($g_cgival["op"] != "up" and $g_cgival["op"] != "down")
	{
		throw new Exception("参数错误！op");
	}
	
	//id
	cgi::both($g_cgival["id"], "id", "");
	if (!intval($g_cgival["id"]))
	{
		throw new Exception("参数id错误");
	}
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show;
	
	$type = $g_cgival["type"];
	$id = $g_cgival["id"];
	$op = $g_cgival["op"];
	
	// 拿到当前站点的site_id、admin_id、admin_name
	$site_id = admin::admin_get_user_site_id();
	$admin_id = admin::admin_get_login_uid();
	$admin_name = admin::admin_get_login_user_name();
	
	// 根据类型不同判断是达人or焦点图
	if ($type == "daren")
	{
		$db = new db_food_user_daren_index();
	}
	elseif ($type == "focus_pic")
	{
		$db = new db_food_focus_pic_index();
	}
	
	// 查出当前上架的所有内容
	$shangjia_list = $db->admin_get_shangjia($site_id);
	if(count($shangjia_list) == 1)
	{
		return;
	}

	// 组件一个临时数组，用于重排序
	$shangjia_order_data = array();
	$idx = count($shangjia_list);
	$current_idx = 0;
	foreach($shangjia_list as $k =>$v)
	{
		$shangjia_order_data[$idx] = $v;

		if ($v["id"] == $id)
		{
			$current_idx = $idx;
		}
		
		$idx --;		
	}
	
	// 数组重排
	if ($op == "up")
	{
		if ($idx == count($shangjia_order_data))
		{
			return;
		}
		else
		{
			$current_item = $shangjia_order_data[$current_idx];
			$shangjia_order_data[$current_idx] = $shangjia_order_data[$current_idx + 1];
			$shangjia_order_data[$current_idx+1] = $current_item;
		}
	}
	else
	{
		if ($idx == 1)
		{
			return;
		}
		else
		{
			$current_item = $shangjia_order_data[$current_idx];
			$shangjia_order_data[$current_idx] = $shangjia_order_data[$current_idx-1];
			$shangjia_order_data[$current_idx-1] = $current_item;
		}
	}
	
	// 更新数据库
	foreach($shangjia_order_data as $k => $v)
	{
		if (!$v) unset($shangjia_order_data[$k]);
	}
	foreach($shangjia_order_data as $k => $v)
	{
		$id = $v["uid"];
		$idx = $k;
		$db->admin_update_shangjia_order_by_id($id, $site_id, $idx, $admin_id, $admin_name);
	}
}

try
{
	admin_check_login();

	check_cgi_pro();
	get_data();

	$g_show["error"] = 0;
	$g_show["errmsg"] = "成功";	
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit;


function show_pro()
{
	global $g_show;

	echo json_encode($g_show);
}
?>